package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.ReturnType;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.common.mapUtils;
import com.jy.wms.pojo.SysLog;
import com.jy.wms.service.SysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName: SysLogController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author liyi
 * @Date Wed Sep 13 15:46:03 CST 2017
 */
@RestController
@RequestMapping("/sysLog")
@Api(value = "/sysLog", description = "关于系统日志操作")
public class  SysLogController extends BaseController<SysLogService,SysLog> {

    @ApiOperation(value = "系统日志管理 POST请求查询系统日志信息",response = JSONObject.class,
            notes = "查询输入</br>{\"jb\":\"错误级别\",</br>\"jlsj_start\":\"开始时间（毫秒）\",</br>\"jlsj_end\":\"结束时间（毫秒）\"," +
            "</br>\"clzt\":\"处理状态\",</br>\"page\":\"当前页（必填）\",</br>\"rows\":\"显示行数（必填）\"}")
    @PostMapping(value="/list")
    public @ResponseBody JSONObject getSysLog(HttpSession session,@RequestBody Map<String, Object> map) {
        Page<SysLog> list = new Page<SysLog>();
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            if(map.containsKey("jlsj_start") && map.containsKey("jlsj_end")){
                long kssj =  (long)map.get("jlsj_start");
                SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String kssjStr = sdf.format(new Date(kssj));
                map.put("kssj",kssjStr);
                long jssj =  (long)map.get("jlsj_end");
                String jssjStr = sdf.format(new Date(jssj));
                map.put("jssj",jssjStr);
            }
            SysLog sysLog = (SysLog) mapUtils.mapToBean(map,SysLog.class);
            sysLog.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            list= baseService.baseSelectPage(sysLog);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (ServiceException e){
            e.printStackTrace();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(value = "系统日志管理 处理完成",response = JSONObject.class,
            notes = "查询输入</br>{\"clsj\":\"解决时间\",</br>\"ycyy\":\"异常原因\",</br>\"cljg\":\"处理方式\"," +
            "</br>\"clr\":\"处理人\",</br>\"id\":\"当前日志ID\"}")
    @PostMapping(value="/processed")
    public @ResponseBody JSONObject processed(@RequestBody Map<String,Object> map) {
        try {
            SysLog sysLog = new SysLog();
            sysLog.setId(map.get("id").toString());
            long clsj =  (long)map.get("clsj");
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sysLog.setClsj(sdf.format(new Date(clsj)));
            sysLog.setYcyy(map.get("ycyy").toString());
            sysLog.setCljg(map.get("cljg").toString());
            sysLog.setClr(map.get("clr").toString());
            sysLog.setClzt("1");
            baseService.baseUpdateByPrimaryKeySelective(sysLog);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"处理信息填写成功","", "");
        } catch (ServiceException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"处理信息填写失败",e.getMessage(),"");
        }  catch (SucessException e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"处理信息填写失败",e.getMessage(),"");
        } catch (Exception e){
            e.printStackTrace();;
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"处理信息填写失败",e.getMessage(),"");
        }
    }

    @GetMapping(value = "/detail/{id}")
    @ApiOperation(value = "系统日志管理 查看处理详情",response = JSONObject.class, notes = "get请求查询系统日志管理处理详情，'id'系统日志ID")
    public @ResponseBody JSONObject detail(@PathVariable("id") String id) {
        JSONObject json = getJSONObject();
        try {
            SysLog sysLog = baseService.baseSelectByPrimaryKey(id);
            json.put("xx",sysLog.getXx());
            json.put("ycyy",sysLog.getYcyy());
            json.put("cljg",sysLog.getCljg());
            json.put("clr",sysLog.getClr());

        } catch (ServiceException e){
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
}
